// Copyright (c)  2009  John Abbott
// This file is part of the CoCoALib suite of examples.
// You are free to use any part of this example in your own programs.

#include "CoCoA/library.H"
using namespace CoCoA;
using namespace std;

//----------------------------------------------------------------------
const string ShortDescription =
  "This example shows how to interpret the result of a factorization. \n";

const string LongDescription =
  "This example shows how to interpret the result of a factorization. \n"
  "It creates a ring element (belonging to a polynomial ring), and    \n"
  "factorizes it.  The result is a \"factorization object\".  We show \n"
  "how to access the various parts of this object.                    \n";
//----------------------------------------------------------------------
string bool2string(bool b){
if (b) return "\b";
else return "not";
}

void program()
{
  GlobalManager CoCoAFoundations;
  vector<symbol> vars;
  vars.insert(vars.end(),symbol("x"));
  vars.insert(vars.end(),symbol("y"));
  cout << "x1 is " << bool2string(symbol::IsValidHead("x1")) << " a symbol" << endl; 
  cout << "xy is " << bool2string(symbol::IsValidHead("xy")) << " a symbol" << endl; 
  SparsePolyRing P = NewPolyRing(RingQ(),vars) ; // QQ[x,y];
  const vector<RingElem>& x = indets(P);
  RingElem f = power(x[0],96) - power(x[1],96); // f = x^96-y^96
  RingElem g = power(x[0],95) - power(x[1],95); // f = x^96-y^96
  RingElem d = gcd(f,g);
  // FacInfo contains 3 fields:
  //   myFactors         - a vector of irreducible factors
  //   myExponents       - a vector of the corresponding multiplicities
  //   myRemainingFactor - an unfactored part (e.g. the content)

  GlobalOutput() << "The gcd of " << f << " and "<< g << " is:" << d << endl;
}

//----------------------------------------------------------------------
// Use main() to handle any uncaught exceptions and warn the user about them.
int main()
{
  try
  {
    program();
    return 0;
  }
  catch (const CoCoA::ErrorInfo& err)
  {
    GlobalErrput() << "***ERROR***  UNCAUGHT CoCoA error";
    ANNOUNCE(err);
  }
  catch (const std::exception& exc)
  {
    GlobalErrput() << "***ERROR***  UNCAUGHT std::exception: " << exc.what() << endl;
  }
  catch(...)
  {
    GlobalErrput() << "***ERROR***  UNCAUGHT UNKNOWN EXCEPTION" << endl;
  }
  return 1;
}

//----------------------------------------------------------------------
// RCS header/log in the next few lines
// $Header: /Volumes/Home_1/cocoa/cvs-repository/CoCoALib-0.99/examples/ex-factor1.C,v 1.1 2009/09/23 14:09:36 abbott Exp $
// $Log: ex-factor1.C,v $
// Revision 1.1  2009/09/23 14:09:36  abbott
// First example for (polynomial) factorization.
//
//
